using System.Net;
using System.Text;
using System.Web.Mvc;
using System.Xml.Serialization;
using BrettM.IO;
using BrettM.Web.Http;

namespace BrettM.Web.Restful
{
    public class ModelResult<T>
        : ActionResult
    {

        public ModelResult(T o)
        {
            this.Data = o;
        }


        public T Data { get; set; }


        public override void ExecuteResult(ControllerContext context)
        {
            if (Data == null)
            {
                var result = new HttpResult(HttpStatusCode.NotFound);
                result.ExecuteResult(context);
                return;
            }

            if (Data is IETagable)
            {
                var headers = context.HttpContext.Request.Headers;

                var eData = Data as IETagable;

                if( string.Compare( context.HttpContext.Request.HttpMethod, "GET", true ) == 0)
                {
                }


                var requestETag = headers["ETag"];
                if (!string.IsNullOrEmpty(requestETag))
                {
                    var etag = headers["If-Match"];

                    if (!string.IsNullOrEmpty(etag))
                    {
                        if (eData.ETag != headers["If-Match"])
                        {
                            var result = new HttpResult(HttpStatusCode.NotFound);
                            result.ExecuteResult(context);
                            return;

                        }
                    }
                    else if (!string.IsNullOrEmpty(headers["If-None-Match"]))
                    {
                        if (eData.ETag == headers["If-None-Match"])
                        {
                            var result = new HttpResult(HttpStatusCode.PreconditionFailed);
                            result.ExecuteResult(context);
                            return;

                        }

                    }
                    else
                    {

                    }
                }

                
            }

            if ((context.HttpContext.Request.Headers["Accept"] ?? string.Empty).Contains("application/json"))
            {

                new JsonResult() { Data = this.Data, JsonRequestBehavior = JsonRequestBehavior.AllowGet }
                    .ExecuteResult(context);
            }
            else
            {
                //Create our own namespaces for the output
                //var ns = new XmlSerializerNamespaces();
                //ns.Add("", "");

                var serializer = new XmlSerializer(typeof(T));
                using (var writer = new EncodedStringWriter(Encoding.UTF8))
                {
                    serializer.Serialize(writer, this.Data);


                    new ContentResult()
                    {
                        ContentType = "application/xml",
                        ContentEncoding = Encoding.UTF8,
                        Content = writer.ToString()
                    }
                        .ExecuteResult(context);

                }
            }
        }
    }
}
